package com.warehouseprovider.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.warehouseprovider.dao.ZStockMapper;
import entity.ResponseMessage;
import entity.StatusCode;
import entity.Stock;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import service.ZStockService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ZStockServiceImpl implements ZStockService {
    private Logger logger=Logger.getLogger(this.getClass());

    @Resource
    private ZStockMapper zStockMapper;

    /**
     * 查询库存
     * 模糊查询商品名字 pname
     * 库名
     * @param pname
     * @param type
     * @return
     */
    @Override
    public ResponseMessage getFyStock(String pname, Integer type,int pageNo,int pageSize) {
        logger.info("ZStockServiceImpl getFyStock pname:"+pname+",type:"+type+",pageNo:"+pageNo+",pageSize:"+pageSize);
        ResponseMessage msg=new ResponseMessage();
        List<Stock> list = new ArrayList();
        Map map=new HashMap();
        map.put("pname",pname);
        map.put("type",type);
        PageHelper.startPage(pageNo,pageSize);
        list=zStockMapper.getFyStock(map);
        logger.debug("ZStockServiceImpl getFyStock map:"+map+",list:"+list);
        try {
            if(list==null){
                logger.error("ZStockServiceImpl getFyStock is error,params:"+map);
                msg.setFlag(false);
                msg.setMessage("查询库存失败！");
                msg.setCode(StatusCode.ERROR);
                return msg;
            }
        } catch (Exception e) {
            logger.error("ZStockServiceImpl getFyStock is error",e);
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("查询库存失败！进入catch..");
            return msg;
        }
        logger.debug("ZStockServiceImpl getFyStock is success,param:"+map+"result:"+list);
        msg.setMessage("查询库存成功！");
        msg.setFlag(true);
        msg.setCode(StatusCode.OK);
        msg.setData(new PageInfo<>(list));
        return msg;
    }
}
